package com.linkgie.galaxyframework.shell.utils;

public class BooleanInputAcceptor implements InputAcceptor {

    private String[] trueValues;
    private String[] falseValues;
    private boolean ignoreCase;
    private boolean nullMatchAny;


    

    /**
     * @param trueValues
     * @param falseValues
     * @param ignoreCase
     * @param nullMatchAny
     */
    public BooleanInputAcceptor(String[] trueValues, String[] falseValues, boolean ignoreCase, boolean nullMatchAny) {
        this.trueValues = trueValues;
        this.falseValues = falseValues;
        this.ignoreCase = ignoreCase;
        this.nullMatchAny = nullMatchAny;
    }

    @Override
    public Acception accept(String input) {
        if (trueValues != null) {
            if (contains(trueValues, input, ignoreCase)) {
                return Acception.accept(true);
            }
        }
        if (falseValues != null) {
            if (contains(falseValues, input, ignoreCase)) {
                return Acception.accept(false);
            }
        }
        if (nullMatchAny) {
            if (trueValues == null && falseValues != null) {
                return Acception.accept(true);
            }
            if (trueValues != null && falseValues == null) {
                return Acception.accept(false);
            }
        }
        return Acception.rejectInWarning("无效输入！");
    }

    private boolean contains(String[] values, String input, boolean ignoreCase) {
        if (ignoreCase) {
            for (String v : values) {
                if (input.equalsIgnoreCase(v)) {
                    return true;
                }
            }
        } else {
            for (String v : values) {
                if (input.equals(v)) {
                    return true;
                }
            }
        }
        return false;
    }

}
